#!/usr/bin/env bash

set -Eeuo pipefail

# Run nginx as root
sed -i 's/user  nginx.*$//' /etc/nginx/nginx.conf

# Write selected env vars to nginx config
envsubst '$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf

# Inject runtime config into the client
function inject_config() {
  # Read runtime config from env in the same way as the @backstage/config-loader package
  local config
  config="$(jq -n 'env |
    with_entries(select(.key | startswith("APP_CONFIG_")) | .key |= sub("APP_CONFIG_"; "")) |
    to_entries |
    reduce .[] as $item (
      {}; setpath($item.key | split("_"); $item.value | fromjson)
    )')"

  >&2 echo "Runtime app config: $config"

  local main_js
  main_js="$(grep -l __APP_INJECTED_RUNTIME_CONFIG__ /usr/share/nginx/html/*.chunk.js)"
  echo "Writing runtime config to ${main_js}"

  # escape ' and " twice, for both sed and json
  local config_escaped_1
  config_escaped_1="$(echo "$config" | jq -cM . | sed -e 's/[\\"\x27]/\\&/g')" # \x27 = '
  # escape / and & for sed
  local config_escaped_2
  config_escaped_2="$(echo "$config_escaped_1" | sed -e 's/[\/&]/\\&/g')"

  # Replace __APP_INJECTED_RUNTIME_CONFIG__ in the main chunk with the runtime config
  sed -e "s/__APP_INJECTED_RUNTIME_CONFIG__/$config_escaped_2/" -i "$main_js"
}

inject_config

exec nginx -g 'daemon off;'
